package com.orm.mybatis.serviceImpl;


import com.orm.mybatis.entity.Permission;
import com.orm.mybatis.entity.Role;
import com.orm.mybatis.entity.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class LoginServiceImpl {

    @Resource
    private PermissionsServiceImpl permissionsService;

    @Resource
    private RoleServiceImpl roleService;

    @Resource
    private UserServiceImpl userService;

    /**
     *
     * @param userName
     * @return
     */
    public User getUserByName(String userName){
        User user = userService.queryByName(userName);
        System.out.println("start"+user);
        List<Long> roleLongIds = roleService.getRoleIdListByUserId(user.getId());
        Set<Role> roleSet = new HashSet<>();

        roleLongIds.forEach(
                (v)->{
                 Role role = roleService.queryById(v);

                    List<Long> permissionLongIds = new ArrayList<>();
                    Set<Permission> permissionSet = new HashSet<>();
                    permissionLongIds =  permissionsService.getPermissionIdListByRoleId(v);
                    permissionLongIds.forEach(
                            (v1)->{
                               permissionSet.add(permissionsService.queryById(v1));
                            }
                    );
                    role.setPermission(permissionSet);
                    roleSet.add(role);
                }
        );
        user.setRoles(roleSet);
      return user;
    }

}
